class MobileController < ApplicationController
  
  skip_before_filter	:verify_authenticity_token 
	before_filter :login_required
  
  def index
    render :text => "hello world"
  end
  
  def hello
    @message = "hello world"
    @page_title = "Exmamples"
  end
  
  def location
    @page_title = "人员管理 -> 人员分布"
  end
  
  def device
    @page_title = "设备管理 -> 设备登记"
  end
  
  
  def get_all_user
    users = Users.find_by_sql("select * from locations;")
    render :text => users.to_json
  end
  
  def query_user
    lon,lat = params['lon'],params['lat'];
    users = Users.find_by_sql("select * from locations where distance(the_geom,geomFromText('Point(#{lon} #{lat})',900913)) < 500;")
    render :text => users.to_json
  end
  
  
  def get_user_points
    users = Users.find_by_sql("select * from locpoints where ygid=#{params['ygid']} order by id desc limit 100;")
    render :text => users.to_json
  end
  
  def get_locations
    user = Users.find_by_sql("select * from locations order by update_at desc, id desc;")
		size = user.size;
		if size > 0 
			txt = "{results:#{size},rows:["
			for k in 0..user.size-1
				txt = txt + user[k].to_json[9..-2] + ','
			end
			txt = txt[0..-2] + "]}"
		else
			txt = "{results:0,rows:[]}"	 
		end	 
		render :text => txt
  end
end
